﻿/// <reference path="../General/Events/EventSender.js" />
/// <reference path="Turbo.js" />

var $ParameterEventArgs = TA.Turbo.Common.ParameterEventArgs = function ParameterEventArgs(param)
{
	this._param = param;
}
$ParameterEventArgs.prototype._param = null;

$ParameterEventArgs.prototype.get_Parameter = function get_Parameter()
{
	return this._param;
}


var $ServerEventSender = TA.Turbo.Common.ServerEventSender = function ServerEventSender()
{
	this.EventSender();
}
$ServerEventSender.Inherit(EventSender);

var ____a;
if (____a)
{
	$ServerEventSender.prototype = new EventSender();
	$ServerEventSender.prototype.EventSender = new EventSender();
}

$ServerEventSender.prototype.AddServerEvent = function AddServerEvent(name, controlServerName, postScenarioId)
{
	var _name = name, _controlServerName = controlServerName, _postScenarioId = postScenarioId;
	this.AddEvent(name, function(args)
	{
		var sb = new StringBuilder();
		var callback = null;

		if (args != null)
		{
			if (args.CallBack != null)
			{
				callback = args.CallBack;
				delete args.CallBack;
			}
			
			if (Function.Is(args, $ParameterEventArgs))
			{
				sb.Append(args.get_Parameter());
			}
			else
			{
				var jw = new JsonWriter(sb);
				if (Function.Is(args, IJsonSerializable))
				{
					args.Serialize(jw);
				}
				else
				{
					var tempArgs = new Object();
					for (var p in args)
					{
						if (
					typeof (args[p]) == "number"
					|| typeof (args[p]) == "string"
					|| typeof (args[p]) == "boolean"
					|| args[p] instanceof Array
					|| args[p] instanceof Date
				)
						{
							tempArgs[p] = args[p];
						}
					}
					Object.Serialize(tempArgs, jw);
				}
			}
		}

		$DoPostBack(_controlServerName, _name, sb.toString(), _postScenarioId, callback);
	});
}

$ServerEventSender.prototype.RaiseServerEvent = function RaiseServerEvent(name, args, callback)
{
	if (typeof (args) == "string")
	{
		args = new $ParameterEventArgs(args);
	}
	if (args != null)
	{
		args.CallBack = callback;
	}
	this.RaiseEvent(name, args);
}
